package com.besall.allbase.view.activity.chipstoollevel4.protocol.FitnessParams;

import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_HRZONE_MODE;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_HRZONE_VALUE;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_SPORTS_CONDITION;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_SPORTS_MODE;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;
import com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;

public class HRZoneValue extends DialogFragment {
    public int v = 0;
    public int mode = 0;
    String[] options = {"ENDURANCE","ANAEROBIC","RECOVERY","FAT BURN","THRESHOLD"};
    private ConfigCallBack mConfigCallback;
    EditText ValueEd;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.config_hrzonevalue, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){
        ValueEd = (EditText) view.findViewById(R.id.value);
        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);
        mode = (int) SPHelper.getPreference(getActivity(), KEY_HRZONE_MODE,0);
        LoopView date = (LoopView) view.findViewById(R.id.date);
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add(options[i]);
        }
        date.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mode = index;
            }
        });
        date.setItems(list);
        date.setInitPosition(mode);
    }

    public boolean saveConfig(){
        String HRvalue = ValueEd.getText().toString();
        if (TextUtils.isEmpty(HRvalue)) {
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 116", Toast.LENGTH_SHORT).show();
            return false;
        }
        v = Integer.valueOf(HRvalue);
        if (v > 116){
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 116", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), KEY_HRZONE_MODE, mode);
        SPHelper.putPreference(getActivity(), KEY_HRZONE_VALUE, v);
        return true;
    }



    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }

    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };


    public interface ConfigCallBack {

        void onConfigOk();

        void onConfigCancel();
    }



}
